package com.zch.action;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.base.dialect.PaginationSupport;
import com.base.dao.model.Ret;
import com.base.dao.model.Grid;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.apache.commons.lang.StringUtils;
import com.base.util.JSONUtils;
import com.base.web.annotation.LoginFilter;
import com.item.service.MessageService;

import org.springframework.beans.factory.annotation.Autowired;
import com.base.action.CoreController;

import com.zch.dao.model.ProjectAnalyze;
import com.zch.dao.model.ProjectAnalyzeExample;
import com.zch.dao.model.ProjectConnect;
import com.zch.dao.model.ProjectMessage;
import com.zch.daoEx.model.ProjectAnalyzeEx;
import com.zch.service.ProjectAnalyzeService;
import com.zch.service.ProjectMessageService;

@RequestMapping("projectAnalyze")
@Controller
@LoginFilter
public class ProjectAnalyzeController extends CoreController{

	@Autowired
	private ProjectAnalyzeService projectAnalyzeService;
	@Autowired
	private ProjectMessageService projectMessageService;
	@Autowired
    private MessageService messageService;

	@RequestMapping("/list")
	@ResponseBody
	public String list(Integer page, Integer rows,String name){
		PaginationSupport.byPage(page, rows);
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("name", name);
		List<ProjectAnalyzeEx> list = projectAnalyzeService.selectList(map);
		return JSONUtils.serialize(new Grid(PaginationSupport.getContext().getTotalCount(), list));
	}

	@RequestMapping("/save")
	@ResponseBody
	public String save(ProjectAnalyze projectAnalyze){
		if (StringUtils.isBlank(projectAnalyze.getId())){
			projectAnalyzeService.insert(projectAnalyze);
		}else{
			projectAnalyzeService.updateByPrimaryKeySelective(projectAnalyze);
			
			projectAnalyze = projectAnalyzeService.selectByPrimaryKey(projectAnalyze.getId());
			String projectId  =projectAnalyze.getProjectId();
			
			ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(projectId);
            String pushContent = "";
            if(projectAnalyze.getIsAgree()==2){
                pushContent = "审核未通过!原因"+projectAnalyze.getNotAgreeReason();
                projectMessage.setNotAgreeReason(projectAnalyze.getNotAgreeReason());
                projectMessage.setIsEnable(1);
                projectMessage.setIsAgree(2);
                projectMessageService.updateByPrimaryKeySelective(projectMessage);
                messageService.findMessage(projectMessage.getTitle(), pushContent, projectMessage.getUserId(), 1);
            }
               
		}
		return JSONUtils.serialize(new Ret(0));
	}

	@RequestMapping("/findById")
	@ResponseBody
	public String find(String id){
		ProjectAnalyze projectAnalyze = projectAnalyzeService.selectByPrimaryKey(id);
		projectAnalyze.setContent(projectAnalyze.getContent().replaceAll("\n", "<br/>").replaceAll("\\s", "&nbsp;"));
		return JSONUtils.serialize(projectAnalyze);
	}

	@RequestMapping("/del")
	@ResponseBody
	public String del(String id){
		String[] ids = id.split(",");
		for (String str : ids){
			projectAnalyzeService.deleteByPrimaryKey(str);
		}
		return JSONUtils.serialize(new Ret(0));
	}
	
    @RequestMapping("/update")
    @ResponseBody
    public String update(ProjectAnalyze projectAnalyze){
        this.projectAnalyzeService.updateByPrimaryKeySelective(projectAnalyze);
        return JSONUtils.serialize(new Ret(0));
    }
    
    @RequestMapping("/saveConnectMaster")
    @ResponseBody
    public String saveConnectMaster(String  userId,String id){
        if (StringUtils.isNotBlank(userId)){
            int result = projectAnalyzeService.updateConnectMaster(userId,id);
            if(result == 1){
                ProjectAnalyze projectAnalyze = projectAnalyzeService.selectByPrimaryKey(id);
                ProjectMessage projectMessage = projectMessageService.selectByPrimaryKey(projectAnalyze.getProjectId());
                //推送给发布人
                messageService.findMessage(projectMessage.getTitle(), "审核通过！", projectMessage.getUserId(), 1);
                
                //推送给可行性分析人员
                String pushContent2 = "您有一个项目需要对接！";
                messageService.findMessage(projectMessage.getTitle(), pushContent2, userId, 2);
            }
        }
        return JSONUtils.serialize(new Ret(0));
    } 
}